<script>
	function submit_topic_form() {
		if ($('#topic_form input[name=preview]').val() == 1) {
			return true;
		} else {
			$('#topic_form_ajax').css('visibility', 'visible');
			$.post(
				'<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'ajax_add_topic'))); ?>',
				$('#topic_form').serialize(),
				function(data) {
					if (data.status == 'error') {
						$('#topic_form_msgbox').html('<div class="msg msg-error">'+data.message+'</div>');
						$('#message_form_ajax').css('visibility', 'hidden');
					} else {
						var redirect = '<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'goto', 'id' => '{message_id}'))); ?>';
						redirect = redirect.replace('{message_id}', data.message_id);
						setTimeout(function() {
							window.location.href = mcmf_route_redirect.replace('{url}', base64_encode(redirect));
						}, mcmf_timeout_default);
						$('#topic_form_msgbox').html('<div class="msg msg-success">Тема создана. Перенаправление...</div>');
						$('#topic_form_ajax').css('visibility', 'hidden');
					}
				}, 'JSON'
			);
			return false;
		}
	}
</script>

<div class="padding bodyhead">
	<p class="pagination crumbs clearfix">
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(), TRUE); ?>" title="Форум Nissan Cedric">Форум</a>
		&raquo;
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'section', 'id' => $section->id)), TRUE); ?>" title="<?php echo HTML::chars($section->name); ?>"><?php echo $section->name; ?></a>
		&raquo;
	</p>
	<h1><?php echo $cat->name; ?></h1>
</div>

<?php if (count($topics) < 1): ?>
	<div class="padding">
		<div class="msg msg-info">Пока что не создано ни одной темы.</div>
	</div>
<?php else: ?>
	<div class="padding">
		<div class="w100 clearfix" style="margin-bottom: -8px;">
			<?php echo $pagination->render(); ?>
			<noindex>
				<p class="pagination forum-buttons fright">
					<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $cat->id)), TRUE); ?>#topic_form_start">Открыть тему</a>
				</p>
			</noindex>
		</div>
		<table class="forum forum_topic forum_cat">
			<thead>
				<tr>
					<td class="fsmini" colspan="2">Список тем</td>
					<td class="fsmini">Автор</td>
					<td class="fsmini wsn">Отв./Просм.</td>
					<td class="fsmini" style="width: 200px;">Последнее сообщение</td>
				</tr>
			</thead>
			<tbody>
				<?php foreach ($topics as $topic): ?>
				<tr>
					<td class="ico altrow"><?php
						if ($topic->open == 0)
							echo '<span class="forum-ico forum-ico-closed" title="Тема закрыта"></span>';
						elseif (Model_Reg::get('user')->id == '' OR $topic->unreaded->where('user_id', '=', Model_Reg::get('user')->id)->count_all() < 1)
							echo '<span class="forum-ico forum-ico-posts-off" title="Нет новых сообщений"></span>';
						else
							echo '<span class="forum-ico forum-ico-posts" title="Есть новые сообщения"></span>';
					?></td>
					<td>
						<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array(
							'action' => 'topic',
							'id' => $topic->id,
						))); ?>" <?php if (Model_Reg::get('user')->id == '' OR $topic->unreaded->where('user_id', '=', Model_Reg::get('user')->id)->count_all() < 1) echo 'class="fclight"'; ?>><?php echo $topic->name; ?></a>
						&nbsp;
						<?php echo View::factory('frontend/snippet/forum.table.topic.pagination')->set('topic', $topic)->set('total_messages', $pagination->total_items)->render(); ?>
					</td>
					<td class="w16px wsn altrow fsmini"><a href="<?php echo URL::site(Route::get('frontend_user')->uri(array(
						'action' => 'view',
						'id' => $topic->user->id,
					)), TRUE); ?>"><?php echo Model_User::nickname($topic->user); ?></a></td>
					<td class="tac w16px wsn altrow altrow2 fsmini"><span class="fcred"><?php echo intval($topic->messages->count_all()) - 1; ?></span> / <?php echo intval($topic->views->count_all()); ?></td>
					<td class="tal w16px fsmini"><?php
						$lastmessage = $topic->messages->order_by('date', 'DESC')->limit(1)->find();
						if (isset($lastmessage->id)):
							echo '<span>'.date('d.m.Y в H:i', $lastmessage->date).'<br /></span>';
							echo '<span>от <a class="fcnormal" href="'.URL::site(Route::get('frontend_forum')->uri(array(
								'action' => 'goto',
								'id' => $lastmessage->id,
							))).'">'.Model_User::nickname($lastmessage->user).'</a></span>';
						else:
							echo '<span>&mdash;</span>';
						endif;
					?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<div class="w100 clearfix" style="margin-bottom: -8px;">
			<?php echo $pagination->render(); ?>
			<noindex>
				<p class="pagination forum-buttons fright">
					<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $cat->id)), TRUE); ?>#topic_form_start">Открыть тему</a>
				</p>
			</noindex>
		</div>
	</div>
<?php endif; ?>

<div id="topic_form_start" class="padding">
	<div id="topic_form_msgbox"></div>
	<form id="topic_form" class="default" action="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'previewtopic')), TRUE); ?>#topic_form_start" method="post" onsubmit="return submit_topic_form();">
		<fieldset>
			<legend>Создать новую тему</legend>
			<input type="hidden" name="preview" value="0" />
			<input type="hidden" name="topic[section_id]" value="<?php echo $section->id; ?>" />
			<input type="hidden" name="topic[cat_id]" value="<?php echo $cat->id; ?>" />
			<table class="cols">
				<tr>
					<td class="col col-topic-form-fields">
						<div class="row">
							<div class="label">Название темы</div>
							<div class="field"><input type="text" name="topic[name]" value="" /></div>
						</div>
						<div class="row">
							<div class="field"><textarea id="markItUp" name="message[text]"></textarea></div>
							<script>$('#markItUp').markItUp(markitupSettings).css('width', '557px');</script>
						</div>
						<div class="row row-button">
							<input type="submit" name="save" value="Предпросмотр" onclick="$('#topic_form input[name=preview]').val('1');" />
							<input type="submit" name="save" value="Опубликовать" onclick="$('#topic_form input[name=preview]').val('0');" />
							<img id="message_form_ajax" class="ajax_icon" src="/media/img/ajax.gif" alt="" />
							<label class="box fssmall"><input type="checkbox" name="message[subscribe]" value="1" checked="checked" /> Получать комментарии на e-mail</label>
						</div>
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
</div>
